/*

Input: newdata/estimation_prep_ltw18 [prepared precinct-level panel]
	
Output: Figure C.2

Tasks: Heterogeneity by reason of reassignment

	
*/	
	
* PULL: Precinct-level data
	use "$newdata/estimation_prep_ltw18.dta", clear
	
********************************************************************************
		// Prep Estimation //
********************************************************************************			
	// compute group ids (ind_treat_consol ind_treat_no_consol) 
	cap drop tmp*
	gen 	tmp = (treat_consol>0& treat_consol>treat_no_consol) 	if K50==0
	bys sb_new (tmp): gen ind_treat_consol = tmp[1]
	replace ind_treat_consol = 0 									if missing(Ei50)
	lab var ind_treat_consol "=1 if event due to reconfiguration, 0 else"
	
	cap drop tmp*
	gen 	tmp = (treat_no_consol>0& treat_consol<treat_no_consol) if K50==0
	bys sb_new (tmp): gen ind_treat_no_consol = tmp[1]
	replace ind_treat_no_consol = 0 								if missing(Ei50)	
	lab var ind_treat_no_consol "=1 if event due to recruitment, 0 else"
		

********************************************************************************
	// Heterogeneity by reason of reassignment (Figure C2) //
********************************************************************************		
	

	// gen leads and lags
	cap drop L* F*
	forvalues l = 7(-1)1 {
		gen F`l'event = K50==-`l'
	}	
	forvalues l = 0/7 {
		gen L`l'event = K50==`l'
	}
	
	// Create two set of dummies: Reason Dummy x rel. time dummies
	forvalues l = 7(-1)1 {		
		gen 	F`l'event_a = F`l'event *ind_treat_no_consol	// a := Recruitment
		gen	 	F`l'event_b = F`l'event *ind_treat_consol		// b:= Reconfig
		assert  F`l'event_b+F`l'event_a==F`l'event		
		
	}
	forvalues l = 0/7 {		
		gen 	L`l'event_a = L`l'event *ind_treat_no_consol	// a := Recruitment
		gen	 	L`l'event_b = L`l'event *ind_treat_consol		// b:= Reconfig
		assert  L`l'event_b+L`l'event_a==L`l'event		
		
	}		
	
	// ORDER dummies
	order *event_b, last
	order F1event*,last	
	
	
	// Estimate ES by reason of reassignment
	estimates clear
	foreach v of varlist turnout_urne turnout_pos_req turnout_tot_req del_street_dist treat_simple{
	
		reghdfe `v' F7event_a-L7event_a  F7event_b-L7event_b F1event_a F1event_b ///
				$ctr $wgt if smpl_trim50 ==1, absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)		
		
		estimates store `v'_a
		estimates store `v'_b	

		
		// PLOT Outcome + save
		event_plot  `v'_a `v'_b , ///
		stub_lag(L#event_a L#event_b ) stub_lead(F#event_a F#event_b ) plottype(connect) ciplottype(rcap) ///
		together perturb(-0.1(0.2)0.1) trimlead(4) trimlag(2) noautolegend ///
		graph_opt(xtitle("") ytitle("Voter turnout in %""(estimates)", size(small)) xlabel(-4(1)2) xtitle("Election since reassignment") ///
			legend(pos(12) order(1 "Polling location recruitment" 3 "Precinct reconfiguration" ) row(1) region(style(none)) bexpand just(left) span) ///
			xline(-0.5, lcolor(black) lpattern(solid)) yline(0, lcolor(gray) lpat(solid)) ylabel(, angle(horizontal)) ///
			name(`v', replace)) ///
		lag_opt1(msymbol(S) msize(2.5pt) color(black)) 	lag_ci_opt1(color(black)) ///
		lag_opt2(msymbol(O) msize(2.5pt) color(maroon)) lag_ci_opt2(color(maroon)) 	 		 	
	}
	
	// Test and plot differences in estimates
	foreach q in turnout_urne_a turnout_pos_req_a turnout_tot_req_a {
		cap frame drop orsCI
		frame create orsCI s est lb ub
		
		estimates restore `q'
		forvalues s= -4/2 {
			if `s'<0{
				local t=`s'*(-1)
				 lincom F`t'event_b-F`t'event_a
			}
			else {
				 lincom L`s'event_b-L`s'event_a
			}
			 frame post orsCI (`s') (`r(estimate)') (`r(lb)') (`r(ub)')
		}	
		frame orsCI {
			isid s, sort
			format est lb ub %3.0f

			list, noobs clean		
			graph twoway (connect est s , sort lcol(gray) ms(T) msize(2.5pt) mcol(black)) (rcap lb ub s, col(black)) ///
				, xtitle("") ytitle("Difference between estimates", size(small)) xlabel(-4(1)2) xtitle("Election since reassignment") ///
				xline(-0.5, lcol(black) lpat(solid)) yline(0, lcolor(gray) lpat(solid)) name(test_`q', replace) ///
				legend(order(1 "Difference between estimates") pos(12))
		}		
		
	}
				
	
	* PLOT: FIGURE C2. Effect Heterogeneity by Reassignment Reason	
	graph combine turnout_urne test_turnout_urne_a, row(1) 		imargins(small) xcommon ycommon name(gr_urne, replace) ///
		title("{bf:Panel A.} Effect on Polling Place Turnout", nobox span bexpand justification(left) size(small) )
	graph combine turnout_pos_req test_turnout_pos_req_a, row(1) imargins(small) xcommon ycommon name(gr_pos, replace) ///
		title("{bf:Panel B.} Effect on Mail-in Turnout", nobox span bexpand justification(left) size(small) )
	graph combine turnout_tot_req test_turnout_tot_req_a, row(1) imargins(small) xcommon ycommon name(gr_tot, replace) ///
		title("{bf:Panel C.} Effect on Total Turnout", nobox span bexpand justification(left) size(small) )
	
	graph combine gr_urne gr_pos gr_tot, xcommon col(1) imargins(small)
	gr_edit .style.editstyle declared_ysize(6.5) editcopy
	gr_edit .plotregion1.graph3.plotregion1.graph2.yaxis1.edit_tick 2 -0.5 `"-.5"', tickset(major)
	gr_edit .plotregion1.graph3.plotregion1.graph2.yaxis1.edit_tick 3 0.5 `".5"', tickset(major)	

	graph export "$figures/Figure_C2_ES_het_by_reason.pdf", replace
